﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ProjBackEnd.Areas.BizProj.Models;
using ProjBackEnd.Lib;
using ProjBackEnd.DAL;

namespace ProjBackEnd.Areas.BizProj.Controllers
{
    /// <summary>
    /// 项目操作
    /// </summary>
    public class ProjOptController : ApiController
    {
        /// <summary>
        /// 获取项目列表（用作业务展示）
        /// eg. : 0000000001|TST1|WEB|10|1|||2018-01-01|2018-04-01|0,1,9|
        /// </summary>
        /// <param name="key">memberid|comcd（空值=所有）|登录平台（WEB/PC/APP）|每页记录数|当前页数（从1开始）|项目id（0=所有）|客户id（空值=所有）|项目开始时间（空值=所有）|项目预计结束时间（空值=所有）|Pid（0,1,2,...）空值=所有|负责人id（空值=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/bizproj/projopt")]
        public RespProj GetProj(string key)
        {
            RespProj resp = new RespProj();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 11)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string[] strsPid = strsKeyOriginal[9].Split(',');
            string strMgrid = strsKeyOriginal[10];

            int iPageSize, iCurrPage, iCusid,iProjid;
            if (!int.TryParse(strsKeyOriginal[3], out iPageSize) || !int.TryParse(strsKeyOriginal[4], out iCurrPage)||!int.TryParse(strsKeyOriginal[5],out iProjid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            if (iCurrPage == 0)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。当前页码错误";
                return resp;
            }
            int.TryParse(strsKeyOriginal[6], out iCusid);

            DateTime? dtSdt = null;
            DateTime? dtEdt = null;
            try
            {
             
                if (!string.IsNullOrEmpty(strsKeyOriginal[7])) dtSdt = DateTime.Parse(strsKeyOriginal[7]);
                if (!string.IsNullOrEmpty(strsKeyOriginal[8])) dtEdt = DateTime.Parse(strsKeyOriginal[8]).Date.AddDays(1);
            }
            catch
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }
            if (strsPid.Length == 1)
            {
                if (strsPid[0] == "") strsPid = new string[] { };
            }
            resp = projLogic.GetProjListPaging(strMemberid, strComcd, iPageSize, iCurrPage, iProjid, iCusid, dtSdt, dtEdt, strsPid, strMgrid);
            return resp;
        }

        /// <summary>
        /// 获取项目列表（用作赋权展示）
        /// eg. : 0000000001|TST1|WEB|10|1|||2018-01-01|2018-04-01|0,1,9|
        /// </summary>
        /// <param name="key">memberid|comcd（空值=所有）|登录平台（WEB/PC/APP）|每页记录数|当前页数（从1开始）|项目id（0=所有）|客户id（空值=所有）|项目开始时间（空值=所有）|项目预计结束时间（空值=所有）|Pid（0,1,2,...）空值=所有|负责人id（空值=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/bizproj/purprojopt")]
        public RespPurProj GetPurProj(string key)
        {
            RespPurProj resp = new RespPurProj();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 11)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string[] strsPid = strsKeyOriginal[9].Split(',');
            string strMgrid = strsKeyOriginal[10];

            int iPageSize, iCurrPage, iCusid, iProjid;
            if (!int.TryParse(strsKeyOriginal[3], out iPageSize) || !int.TryParse(strsKeyOriginal[4], out iCurrPage) || !int.TryParse(strsKeyOriginal[5], out iProjid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            if (iCurrPage == 0)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。当前页码错误";
                return resp;
            }
            int.TryParse(strsKeyOriginal[6], out iCusid);

            DateTime? dtSdt = null;
            DateTime? dtEdt = null;
            try
            {

                if (!string.IsNullOrEmpty(strsKeyOriginal[7])) dtSdt = DateTime.Parse(strsKeyOriginal[7]);
                if (!string.IsNullOrEmpty(strsKeyOriginal[8])) dtEdt = DateTime.Parse(strsKeyOriginal[8]).Date.AddDays(1);
            }
            catch
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }
            if (strsPid.Length == 1)
            {
                if (strsPid[0] == "") strsPid = new string[] { };
            }

            resp = projLogic.GetPurProjListPaging(strMemberid, strComcd, iPageSize, iCurrPage, iProjid, iCusid, dtSdt, dtEdt, strsPid, strMgrid);
            return resp;
        }

        /// <summary>
        /// 维护项目基本信息
        /// </summary>
        /// <param name="_reqInfo">更新请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/bizproj/projopt")]
        public RespProjNullResultInfo UpdateProjInfo(ReqProj _reqInfo)
        {
            RespProjNullResultInfo resp = new RespProjNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = projLogic.UpdateProjInfo(_reqInfo.memberid,  _reqInfo.projinfo);
            return resp;
        }

        /// <summary>
        /// 项目敏感信息操作
        /// </summary>
        /// <param name="_reqInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/bizproj/projoptsens")]
        public RespProjNullResultInfo ProjProcess(ReqProjProc _reqInfo)
        {
            RespProjNullResultInfo resp = new RespProjNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = projLogic.UpdateProjInfoSens(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.projcore);
            return resp;
        }



    }
}
